// pages/my/myInfo_address/myInfo_address.js
const API = require('../../../API/api.js');
const app = getApp();
import Dialog from '../../../utils/vant-weapp/dialog/dialog';
import Notify from '../../../utils/vant-weapp/notify/notify';

Page({
    backPage(e) {
        let id = e.currentTarget.dataset.id;
        console.log(id)
        if (this.data.backPage) {
            let goodsId = wx.getStorageSync('goodsId');
            let skuId = wx.getStorageSync('skuId');
            wx.redirectTo({
                url: '../../orderInfo/orderInfo?addressId=' + id + '&goodsId=' + goodsId + '&skuId=' + skuId,
            })
        }
    },
    gotoAddressAdd() {
        if (wx.getStorageSync('addAddress')) {
            wx.navigateTo({
                url: '../../my/myInfo_address_add/myInfo_address_add',
            })
        }
    },
    deleteAddress(e) {
        Dialog.confirm({
            title: '删除',
            message: '确定删除当前地址？',
            asyncClose: true
        })
            .then(() => {
                API.deleteAddress({
                    userId: wx.getStorageSync('userId'),
                    addressId: e.currentTarget.dataset.id
                }).then((res) => {
                    console.log(res)
                    Notify({
                        text: '删除成功',
                        duration: 1000,
                        backgroundColor: '#1989fa'
                    });
                })
                setTimeout(() => {
                    Dialog.close();
                }, 1000);
            }).then(() => {
            API.getAddressList({
                userId: wx.getStorageSync('userId')
            }).then((res) => {
                this.setData({
                    addressList: res.data
                })
            })
        })
            .catch(() => {
                Dialog.close();
            })
    },
    editAddress(e) {
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '../../my/myInfo_address_add/myInfo_address_add?id=' + id,
        })
    },
    /**
     * 页面的初始数据
     */
    data: {
        Height: app.globalData.Height,
        addressList: [],
        backPage: null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options.addAddress);
        if (options.addAddress == '1') {
            wx.setStorageSync('addAddress', options.addAddress)
            this.setData({
                backPage: true
            })
        }
        API.getAddressList({
            userId: wx.getStorageSync('userId')
        }).then((res) => {
            this.setData({
                addressList: res.data
            })
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})
